package com.yanqu.road.dao.impl.channelcenter.tonbingchannel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.tonbingchannel.data.TonBingUserChannelData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TonBingUserChannelDataDaoImpl extends TempDataObjectDao<TonBingUserChannelData> {

    public static String getTonBingUserDataSql = "select * from `t_u_activity_tonbing_channel_data` where `activityId`=? and `userId`=?";

    public TonBingUserChannelData getTonBingUserChannelData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getTonBingUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateTonBingUserDataSql = "update `t_u_activity_tonbing_channel_data` set worshipTimeMap=?,positionMap=?,campId=?,zhanGong=?,skinExchangeMap=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(TonBingUserChannelData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getWorshipTimeMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPositionMap())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getZhanGong()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getSkinExchangeMap())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateTonBingUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertTonBingUserDataSql = "insert into `t_u_activity_tonbing_channel_data`(activityId,userId,worshipTimeMap,positionMap,campId,zhanGong,skinExchangeMap) values (?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(TonBingUserChannelData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getWorshipTimeMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPositionMap())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCampId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getZhanGong()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getSkinExchangeMap())));
        if (getDb().executeNoneQuery(insertTonBingUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected TonBingUserChannelData parseData(ResultSet rs) throws SQLException {
        TonBingUserChannelData data = new TonBingUserChannelData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setCampId(rs.getInt("campId"));
        data.setZhanGong(rs.getLong("zhanGong"));
        data.setSkinExchangeMap(JSON.parseObject(rs.getString("skinExchangeMap"), new TypeReference<Map<Integer, Integer>>() {
        }));
        if(!StringUtils.isNullOrEmpty(rs.getString("positionMap"))){
            data.setPositionMap(JSON.parseObject(rs.getString("positionMap"), new TypeReference<Map<Integer, Integer>>() {
            }));
        }
        if(!StringUtils.isNullOrEmpty(rs.getString("worshipTimeMap"))){
            data.setWorshipTimeMap(JSON.parseObject(rs.getString("worshipTimeMap"), new TypeReference<ConcurrentHashMap<Integer, Long>>() {
            }));
        }

        return data;
    }

}
